package com.sangmo.fts.process.factoring.service;

import com.sangmo.boot.framework.common.errors.ServiceException;
import com.sangmo.boot.framework.common.errors.StandardErrors;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.factoring.interfaces.FfTransferApplyCreation;
import com.sangmo.fts.process.factoring.model.FfContract;
import com.sangmo.fts.process.factoring.model.FfFactoringContract;
import com.sangmo.fts.process.factoring.model.FfLoan;
import com.sangmo.fts.process.factoring.model.FfTransferApply;
import com.sangmo.fts.process.factoring.repo.FfTransferApplyRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FfTransferApplyEntityService extends AuthorityEntityService<FfTransferApply> {

    @Autowired
    private FfTransferApplyRepo ffTransferApplyRepo;

    @Autowired
    private FfFactoringContractService ffFactoringContractService;

    @Autowired
    private FfContractService ffContractService;

    @Autowired
    private FfLoanService ffLoanService;

    @Autowired
    private FileUploadService fileUploadService;

    @Override
    protected IDataViewRepo<FfTransferApply> getRepo() {
        return ffTransferApplyRepo;
    }

    @Override
    protected void onCreate(FfTransferApply entity, Object bo) {
        super.onCreate(entity, bo);
        FfTransferApplyCreation creation = (FfTransferApplyCreation) bo;
        FfFactoringContract contract = ffFactoringContractService.findAvailable(creation.getContractId(), entity.getTrainingId());
        entity.setApplyId(contract.getFinancingId());
        entity.setBankId(contract.getBankId());
        FfLoan loan = ffLoanService.get(contract.getLoanId());
        entity.setReceivableAmount(loan.getReceivableAmount());
        entity.setApprovedAmount(loan.getApprovedAmount());
        FfContract ffContract = ffContractService.get(loan.getContractId());
        entity.setBuyerId(ffContract.getBuyerId());
        entity.setTotalAmount(loan.getContractAmount());
    }

    @Override
    protected void afterCreated(FfTransferApply entity, Object bo) {
        super.afterCreated(entity, bo);
        FfTransferApplyCreation creation = (FfTransferApplyCreation) bo;
        if (null != creation.getApplyConfirmDoc()) {
            fileUploadService.updateRef(creation.getApplyConfirmDoc(), ""+entity.getId(), "FfTransferApply.Apply");
        }
        if (null != creation.getInvoiceConfirmDoc()) {
            fileUploadService.updateRef(creation.getInvoiceConfirmDoc(), ""+entity.getId(), "FfTransferApply.Invoice");
        }
        if (null != creation.getTransferNoticeDoc()) {
            fileUploadService.updateRef(creation.getTransferNoticeDoc(), ""+entity.getId(), "FfTransferApply.Transfer");
        }
    }

    @Override
    protected void onDelete(FfTransferApply entity) {
        super.onDelete(entity);
        if (!FfTransferApply.States.PENDING.equals(entity.getState())) {
            throw new ServiceException(StandardErrors.EXTERNAL_ERROR.getStatus(), "error.ffTransferApply.remove.permit.disabled");
        }
    }

    public void bankConfirm(FfTransferApply transferApply) {
        transferApply.setBankConfirm(true);
        this.save(transferApply);
    }

    public void buyerConfirm(FfTransferApply transferApply) {
        transferApply.setBuyerConfirm(true);
        this.save(transferApply);
    }
}
